// 为了删除指定名称的cookie，可以将其过期时间设定为一个过去的时间
function delCookie(name) {
  const ex = new Date()
  ex.setTime(ex.getTime() - 10000)
  document.cookie = name + '=; expires=' + ex.toGMTString() + ';path=/'
}

//判断用户是否登录
export function isLogin() {
  if (document.cookie.indexOf('auth_sign') >= 0) {
    return true
  }
  return false
}

//清除 Cookie
export function clearCookie() {
  delCookie('zanpiancmsuser_auth')
  delCookie('zanpiancmsuser_auth_sign')
  delCookie('PHPSESSID')
}

//显示隐藏
export function display(id, div, cur) {
  const current = cur ? cur : ''
  id.hover(
    function () {
      $(this).addClass(current)
      div.show()
    },
    function () {
      $(this).removeClass(current)
      div.hide()
    }
  )
}
//Tab切换

export function Tabs(obj, box) {
  obj.click(function () {
    const name = $(this).attr('data-name')
    const objDis = $(`#${name}`)
    box.hide()
    obj.removeClass('current')
    $(this).addClass('current')
    objDis.show()
  })
}

export function scrollPos(obj) {
  obj.click(function () {
    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
      var a = $(this.hash)
      a = (a.length && a) || $('[name=' + this.hash.slice(1) + ']')
      if (a.length) {
        var b = a.offset().top - 10
        $('html,body').animate(
          {
            scrollTop: b,
          },
          300
        )
        return false
      }
    }
  })
}

// 提取字符串第一次出现的数字
export const firstNumber = (str) => {
  const isN = /pv|PV|SP|sp|全集|总集/.exec(str) || []
  if (isN.index === 0) {
    return str
  } else {
    const reg = /\d+/g
    const arr = str.match(reg)
    if (arr) {
      return arr[0]
    } else {
      return str
    }
  }
}
